package com.longteng.lesson.controller;


import com.longteng.lesson.po.Hobby;
import com.longteng.lesson.po.User;
import com.longteng.lesson.view.ExcelView;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.*;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.*;

/**
 * Created by Administrator
 * DATE : 2018/5/6
 * TIME : 23:15
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */

@Controller
public class ViewController {

    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model) {
        model.addAttribute("time", new Date());
        model.addAttribute("k", 203333333);
        model.addAttribute("xxx", false);
        User user = new User();
        user.setUserName("xiaojin");
        user.setSex(1);
        model.addAttribute("user", user);
        List<User> userList = new ArrayList<User>();
        userList.add(user);
        User user1 = new User();
        user1.setUserName("xiaozhou");
        user1.setSex(0);
        userList.add(user1);
        User user2 = new User();
        user2.setUserName("xxx");
        user2.setSex(3);
        userList.add(user2);
        model.addAttribute("userList", userList);
        model.addAttribute("switchValue", "b");
        switch (2) {
            case 1 + 1:
                System.out.println();
                break;
            default:
        }
        return "index";
    }

    @RequestMapping("/userPage")
    public String longtengPage(HttpSession httpSession) {
        Object o = httpSession.getAttribute("user");
        return "userForm";
    }

    @RequestMapping("/addUser")
    public String addUser(@Valid User user,BindingResult bindingResult,Model model) {
        if(bindingResult.hasErrors()){
            FieldError error = bindingResult.getFieldErrors().get(0);
            model.addAttribute("errorMsg", error.getDefaultMessage());
            return "jsPage";
        }
        return "longteng/longtengPage";
    }

    @RequestMapping("/getUser")
    public String getUser(Model model) {
        model.addAttribute("userName", "fuqun");
        User user = new User();
        user.setUserName("xiaozhou");
        user.setSex(0);
        Hobby hobby = new Hobby();
        hobby.setHobbyMsg("test");
        user.setHobby(hobby);
        model.addAttribute("user", user);
        return "userMessage";
    }

    @RequestMapping("/getUser2")
    public ModelAndView getUser2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userMessage");
        modelAndView.addObject("userName", "fuqun");
//        modelAndView.addObject("city","北京");
        User user = new User();
        user.setUserName("xiaozhou");
        user.setSex(0);
        Hobby hobby = new Hobby();
        hobby.setHobbyMsg("test");
        user.setHobby(hobby);
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/mySelfView")
    public ModelAndView mySelfView() {
        ModelAndView modelAndView = new ModelAndView();
        View view = new View() {
            public String getContentType() {
                return "text/html";
            }

            public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
                response.addHeader("content-Type", "text/html;charset=UTF-8");
                response.getWriter().print("自定义view解析器");
            }
        };
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/excelView")
    public ModelAndView excelViewTest(User user) {
        Map<String, Object> model = new HashMap();
        ArrayList<User> userlist = new ArrayList();
        user.setSex(1);
        user.setUserName("taki");
        user.setCodeType("java,python");
        user.setPassword("123123");
        userlist.add(user);
        model.put("userList", userlist);
        View view = new ExcelView();
        ModelAndView modelAndView = new ModelAndView(view, model);
        return modelAndView;
    }

    @RequestMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login/loginSystem");
        return modelAndView;
    }
    @RequestMapping("/validateUser")
    public ModelAndView login(@Valid @ModelAttribute("user") User user, BindingResult bindingResult, Model model) {
        ModelAndView modelAndView = new ModelAndView();
        /*
         * 这个地方会去数据库查询该用户的信息，帐号密码是否正确
         * 返回了这个用户的id
         * */
        if(bindingResult.hasErrors()){
            modelAndView.setViewName("login/loginSystem");
            return modelAndView;
        }
       String password= user.getPassword();

       if(!password.equalsIgnoreCase("123456")){
           modelAndView.setViewName("login/loginSystem");
           bindingResult.rejectValue("password","","密码不正确");
           return modelAndView;
       }
        user.setUserId(1);
        modelAndView.setViewName("login/loginSuccess");
        return modelAndView;
    }

    @RequestMapping("/getUserInfo")
    public @ResponseBody
    User getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        /*
         * 你需要从数据库里面拿到这个用户的所有信息
         * 查询他的订单
         * 通过userId去select * from user where userId =1
         * */
        return user;
    }

    @RequestMapping("/str")
    public  String getStr(HttpServletRequest request,@RequestParam("name") String name){
        System.out.println(request.getParameter("name"));
        System.out.println(name);

        return request.getParameter("name");
    }

}
